import Vue from 'vue'
import Router from 'vue-router'

// 引入一级组件
import DashBoard from './../views/dashboard/DashBoard'
import talk from "@/views/talk/Talk";
// 懒加载，提高性能
const Home = () => import('./../views/home/Home.vue');
const Category = () => import('./../views/category/Category.vue');
const Cart = () => import('./../views/cart/Cart.vue');
const Order = () => import('./../views/order/Order.vue');

// 用户中心
const Mine = () => import('./../views/mine/Mine.vue');
const MineOrder = () => import('./../views/mine/children/MineOrder.vue');
const MyAddress = () => import('./../views/order/children/MyAddress.vue');
const EditAddress = () => import('./../views/order/children/children/EditAddress.vue');
const MyCoupons = () => import('./../views/mine/children/MyCoupons.vue');
const Feedback = () => import('./../views/mine/children/Feedback.vue');
const MyInfo = () => import('./../views/mine/children/MyInfo.vue');
const AppAbout = () => import('./../views/mine/children/AppAbout.vue');
// 引入我的地址组件
const AddAddress = () => import('./../views/order/children/children/AddAddress.vue')
const OrderDetail = () => import('./../views/order/children/OrderDetail.vue')
const GoodsDetail = () => import('../views/home/children/GoodsDetail.vue')
const SeckillDetail = () => import('./../views/home/children/SeckillDetail.vue')
const SearchDetail = () => import('./../views/home/children/SearchDetail.vue')
const SearchGoodsDetail = () => import('./../views/home/children/SearchGoodsDetail.vue')
const Talk = () => import('./../views/talk/Talk.vue')
const BigSaleDetail=()=>import('./../views/home/children/BigSaleDetail.vue')
const PaySuccess=()=>import('./../views/order/children/PaySuccess')
const Refund=()=>import('./../views/order/children/Refund')
const RequestRefund=()=>import('./../views/order/children/RequestRefund')

//订单详情页
const PayStatus=()=>import('./../views/order/children/PayStatus.vue')
Vue.use(Router);
const originalPush = Router.prototype.push

Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
const router = new Router({
    routes: [
        {path: '/', redirect: '/dashboard'},
        {
            path:'/login',
            name:'login',
            component:() => import('../views/login/Login')
        },
        {
            path: '/dashboard',
            name: 'dashboard',
            component: DashBoard,
            children: [
                {path: '/dashboard', redirect: '/dashboard/home'},
                {path: 'home', name: 'home', component: Home, meta: {keepAlive: true}},
                {path: 'category', name: 'category', component: Category, meta: {keepAlive: true}},
                {path: 'Cart', name: 'Cart', component: Cart},
                {path: 'editAddress', component: EditAddress},
                {path: 'addAddress', component: AddAddress},
                {
                    path: 'mine',
                    name: 'mine',
                    component: Mine,
                    children:[
                        {path: 'mineOrder', name:'MineOrder', component: MineOrder,},
                        {path: 'myAddress', name:'myAddress', component: MyAddress,},
                        {path: 'myCoupons', name:'myCoupons', component: MyCoupons,},
                        {path: 'feedback', name:'feedback', component: Feedback,},
                        {path: 'myInfo', name:'myInfo', component: MyInfo,},
                        {path: 'appAbout', name:'appAbout', component: AppAbout,},
                    ]
                },
            ]
        },
        {path: '/goodDetail',
            name:'goodDetail',
            component: GoodsDetail
        },
        {
          path:'/bigSaleDetail',
          name:'bigSaleDetail',
          component:BigSaleDetail
        },
        {path: '/seckillDetail',
            name:'seckillDetail',
            component: SeckillDetail},
        {path: '/searchGoodsDetail',
            name:'searchGoodsDetail',
            component: SearchGoodsDetail},
        {path: '/talk',
            name:'onlineCustomer',
            component: Talk},
        {path: '/searchDetail',
            name:'searchDetail',
            component: SearchDetail},
        {
            path: '/confirmOrder',
            name: 'confirmOrder',
            component: Order,
            children: [
                {
                    path: 'myAddress',
                    name: 'myAddress',
                    component: MyAddress,
                    children: [
                        {path: 'addAddress', name: 'addAddress', component: AddAddress},
                        {path: 'editAddress', name: 'editAddress', component: EditAddress}
                    ]
                }, {
                    path: 'orderDetail',
                    name: 'orderDetail',
                    component: OrderDetail
                }
            ]
        },
        {
            path:'/pay',
            name:'pay',
            component:()=>import('../views/order/children/PayStatus')
        },
        {
            path:'/checkOrder',
            name:'checkOrder',
            component:()=>import('../views/order/children/OrderCheck')
        },
        {
            path:'/payStatus',
            name:'payStatus',
            component:()=>import('../views/order/children/PayStatus')
        },
        {
          path:'/paySuccess',
          name:'paySuccess',
            component:PaySuccess
        },
        {
            path:'/refund',
            name:'refund',
            component:Refund
        },
        {
            path:'/requestRefund',
            name:'/requestRefund',
            component:RequestRefund
        }
    ]
})

export default router;

router.beforeEach(
    (to,from,next)=>{
        console.log(localStorage.getItem("token")+"VVVVVVVVVVVVVVVVVVVVVV")
        if(to.fullPath === '/'||to.fullPath === '/login'|| to.fullPath === '/dashboard/home'||to.fullPath==='/dashboard/category'||to.fullPath==='/searchDetail'||to.name==='searchGoodsDetail'||to.name==='goodDetail'||to.name==='seckillDetail'||to.name==='bigSaleDetail') next();
        else if(localStorage.getItem("token")=== null || localStorage.getItem("token")=== '' || !localStorage.getItem("token")){
            router.push({path:'/login'});
        }else {
            console.log("wskde ")
            next();

        }
    }
)